package com.yida.system.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.yida.common.core.constant.CacheConstants;
import com.yida.common.core.exception.ServiceException;
import com.yida.common.core.utils.DateUtils;
import com.yida.common.redis.service.RedisService;
import com.yida.system.api.domain.SysDataPutInConfig;
import com.yida.system.domain.request.SysDataPutInInterfaceConfigEditRequest;
import com.yida.system.domain.request.SysDataPutInInterfaceConfigSaveRequest;
import com.yida.system.api.vo.SysDataPutInInterfaceVO;
import com.yida.system.mapper.SysDataPutInConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yida.system.mapper.SysDataPutInInterfaceConfigMapper;
import com.yida.system.api.domain.SysDataPutInInterfaceConfig;
import com.yida.system.service.ISysDataPutInInterfaceConfigService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 系统数据接入接口配置Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-04
 */
@Service
public class SysDataPutInInterfaceConfigServiceImpl implements ISysDataPutInInterfaceConfigService {
    @Autowired
    private SysDataPutInInterfaceConfigMapper sysDataPutInInterfaceConfigMapper;

    @Autowired
    private SysDataPutInConfigMapper sysDataPutInConfigMapper;

    @Autowired
    private RedisService redisService;

    /**
     * 查询系统数据接入接口配置
     *
     * @param id 系统数据接入接口配置主键
     * @return 系统数据接入接口配置
     */
    @Override
    public SysDataPutInInterfaceConfig selectSysDataPutInInterfaceConfigById(Long id) {
        return sysDataPutInInterfaceConfigMapper.selectSysDataPutInInterfaceConfigById(id);
    }

    /**
     * 查询系统数据接入接口配置列表
     *
     * @param sysDataPutInInterfaceConfig 系统数据接入接口配置
     * @return 系统数据接入接口配置
     */
    @Override
    public List<SysDataPutInInterfaceConfig> selectSysDataPutInInterfaceConfigList(SysDataPutInInterfaceConfig sysDataPutInInterfaceConfig) {
        return sysDataPutInInterfaceConfigMapper.selectSysDataPutInInterfaceConfigList(sysDataPutInInterfaceConfig);
    }

    /**
     * 新增系统数据接入接口配置
     *
     * @param saveRequest 系统数据接入接口配置
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSysDataPutInInterfaceConfig(SysDataPutInInterfaceConfigSaveRequest saveRequest) {
        // 判断系统数据接入配置是否存在
        SysDataPutInConfig sysDataPutInConfig = sysDataPutInConfigMapper.selectSysDataPutInConfigById(saveRequest.getSysDataPutInId());
        if (ObjectUtil.isEmpty(sysDataPutInConfig)) {
            throw new ServiceException("未查询到对应系统数据接入配置信息");
        }
        // 判断接口编号是否重复
        SysDataPutInInterfaceConfig sysDataPutInInterfaceConfig = sysDataPutInInterfaceConfigMapper.selectSysDataPutInInterfaceConfigByInterfaceCode(saveRequest.getInterfaceCode());
        if (ObjectUtil.isNotEmpty(sysDataPutInInterfaceConfig)) {
            throw new ServiceException("当前接口编号已存在请勿重复添加");
        }
        // 拷贝参数
        SysDataPutInInterfaceConfig saveResult = new SysDataPutInInterfaceConfig();
        BeanUtil.copyProperties(saveRequest, saveResult);
        saveResult.setCreateTime(DateUtils.getNowDate());
        // 存入缓存
        saveCache(sysDataPutInConfig,saveResult);
        return sysDataPutInInterfaceConfigMapper.insertSysDataPutInInterfaceConfig(saveResult);
    }

    /**
     * 修改系统数据接入接口配置
     *
     * @param editRequest 系统数据接入接口配置
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSysDataPutInInterfaceConfig(SysDataPutInInterfaceConfigEditRequest editRequest) {
        // 查询系统数据接入接口配置是否存在
        SysDataPutInInterfaceConfig sysDataPutInInterfaceConfig = sysDataPutInInterfaceConfigMapper.selectSysDataPutInInterfaceConfigById(editRequest.getId());
        if (ObjectUtil.isEmpty(sysDataPutInInterfaceConfig)) {
            throw new ServiceException("未查询到对应系统数据接入接口配置信息");
        }
        SysDataPutInConfig sysDataPutInConfig = sysDataPutInConfigMapper.selectSysDataPutInConfigById(editRequest.getSysDataPutInId());
        // 判断系统数据接入配置是否存在
        if (ObjectUtil.isEmpty(sysDataPutInConfig)) {
            throw new ServiceException("未查询到对应系统数据接入配置信息");
        }
        // 判断接口编号是否修改
        if (!sysDataPutInInterfaceConfig.getInterfaceCode().equals(editRequest.getInterfaceCode())) {
            // 判断接口编号是否重复
            if (ObjectUtil.isNotEmpty(sysDataPutInInterfaceConfigMapper.selectSysDataPutInInterfaceConfigByInterfaceCode(editRequest.getInterfaceCode()))) {
                throw new ServiceException("接口编号已存在请勿重复添加");
            }
        }
        // 删除缓存
        redisService.getCacheObject(CacheConstants.SYS_DATA_PUT_IN
                + CacheConstants.REDIS_SUFFIX
                + sysDataPutInConfig.getConfigKey()
                + CacheConstants.REDIS_SUFFIX
                + sysDataPutInInterfaceConfig.getInterfaceCode());
        // 拷贝参数
        BeanUtil.copyProperties(editRequest, sysDataPutInInterfaceConfig);
        sysDataPutInInterfaceConfig.setUpdateTime(DateUtils.getNowDate());
        // 存入缓存
        saveCache(sysDataPutInConfig,sysDataPutInInterfaceConfig);
        return sysDataPutInInterfaceConfigMapper.updateSysDataPutInInterfaceConfig(sysDataPutInInterfaceConfig);
    }

    /**
     * 批量删除系统数据接入接口配置
     *
     * @param ids 需要删除的系统数据接入接口配置主键
     * @return 结果
     */
    @Override
    public int deleteSysDataPutInInterfaceConfigByIds(Long[] ids) {
        for (Long id : ids) {
            SysDataPutInInterfaceConfig sysDataPutInInterfaceConfig = sysDataPutInInterfaceConfigMapper.selectSysDataPutInInterfaceConfigById(id);
            if(ObjectUtil.isEmpty(sysDataPutInInterfaceConfig)){
                throw new ServiceException("未查询到对应系统数据接入接口配置信息");
            }
            SysDataPutInConfig sysDataPutInConfig = sysDataPutInConfigMapper.selectSysDataPutInConfigById(sysDataPutInInterfaceConfig.getSysDataPutInId());
            if(ObjectUtil.isEmpty(sysDataPutInConfig)){
                throw new ServiceException("未查询到对应系统数据对接配置信息");
            }
            // 删除缓存
            redisService.getCacheObject(CacheConstants.SYS_DATA_PUT_IN
                    + CacheConstants.REDIS_SUFFIX
                    + sysDataPutInConfig.getConfigKey()
                    + CacheConstants.REDIS_SUFFIX
                    + sysDataPutInInterfaceConfig.getInterfaceCode());
        }
        return sysDataPutInInterfaceConfigMapper.deleteSysDataPutInInterfaceConfigByIds(ids);
    }

    /**
     * 删除系统数据接入接口配置信息
     *
     * @param id 系统数据接入接口配置主键
     * @return 结果
     */
    @Override
    public int deleteSysDataPutInInterfaceConfigById(Long id) {
        SysDataPutInInterfaceConfig sysDataPutInInterfaceConfig = sysDataPutInInterfaceConfigMapper.selectSysDataPutInInterfaceConfigById(id);
        if(ObjectUtil.isEmpty(sysDataPutInInterfaceConfig)){
            throw new ServiceException("未查询到对应系统数据接入接口配置信息");
        }
        SysDataPutInConfig sysDataPutInConfig = sysDataPutInConfigMapper.selectSysDataPutInConfigById(sysDataPutInInterfaceConfig.getSysDataPutInId());
        if(ObjectUtil.isEmpty(sysDataPutInConfig)){
            throw new ServiceException("未查询到对应系统数据对接配置信息");
        }
        // 删除缓存
        redisService.getCacheObject(CacheConstants.SYS_DATA_PUT_IN
                + CacheConstants.REDIS_SUFFIX
                + sysDataPutInConfig.getConfigKey()
                + CacheConstants.REDIS_SUFFIX
                + sysDataPutInInterfaceConfig.getInterfaceCode());
        return sysDataPutInInterfaceConfigMapper.deleteSysDataPutInInterfaceConfigById(id);
    }

    /**
     * 保存至缓存
     * @param sysDataPutInConfig 系统数据接入配置
     * @param sysDataPutInInterfaceConfig 系统数据接入接口配置
     */
    private void saveCache(SysDataPutInConfig sysDataPutInConfig,SysDataPutInInterfaceConfig sysDataPutInInterfaceConfig){
        // 存入缓存
        SysDataPutInInterfaceVO sysDataPutInInterfaceVO = new SysDataPutInInterfaceVO();
        sysDataPutInInterfaceVO.setConfigKey(sysDataPutInConfig.getConfigKey());
        sysDataPutInInterfaceVO.setCompanyId(sysDataPutInConfig.getCompanyId());
        sysDataPutInInterfaceVO.setIpUrl(sysDataPutInConfig.getIpUrl());
        sysDataPutInInterfaceVO.setPublicKey(sysDataPutInConfig.getPublicKey());
        sysDataPutInInterfaceVO.setPrivateKey(sysDataPutInConfig.getPrivateKey());
        sysDataPutInInterfaceVO.setInterfaceName(sysDataPutInInterfaceConfig.getInterfaceName());
        sysDataPutInInterfaceVO.setInterfaceUri(sysDataPutInInterfaceConfig.getInterfaceUri());
        sysDataPutInInterfaceVO.setInterfaceCode(sysDataPutInInterfaceConfig.getInterfaceCode());
        sysDataPutInInterfaceVO.setDockingInterfaceConfigId(sysDataPutInInterfaceConfig.getDockingInterfaceConfigId());
        redisService.setCacheObject(CacheConstants.SYS_DATA_PUT_IN
                + CacheConstants.REDIS_SUFFIX
                + sysDataPutInConfig.getConfigKey()
                + CacheConstants.REDIS_SUFFIX
                + sysDataPutInInterfaceConfig.getInterfaceCode(), sysDataPutInInterfaceVO);
    }
}
